//
//  CAccountMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/4/13.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CAccountMgr.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "CAccountModel.h"
#import "MicroDef.h"
@implementation CAccountMgr
//        [db executeUpdate:@"CREATE TABLE ACCOUNT (identifer  INTEGER NOT NULL, accountName varchar(50)  NOT NULL, passWord varchar(50)  NOT NULL, gesturePassword varchar(20) DEFAULT NULL, loginStatus INTEGER DEFAULT 0, PRIMARY KEY (identifer));"];

-(BOOL)insetAccount:(CAccountModel *)account
{
    if(account == nil)
    {
        return NO;
    } else {
        if (M_ISSTRNULL(account.userName)) {
            return NO;
        }
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"INSERT INTO ACCOUNT(accountName, passWord, gesturePassword, loginStatus, showInList,userId,info,img,msgNoticeSet,dept,companyId,loginName,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?,?,?,?,?,?,?,?,?,?,?)", account.userName?account.userName:@"", account.passWord?account.passWord:@"", account.gesturePassword?account.gesturePassword:@"", account.recentlyLogin?@1:@0, account.isShowInList?@1:@0,M_STOREVALUE(account.userId),M_STOREVALUE(account.info),M_STOREVALUE(account.img),M_STOREVALUE(account.msgNoticeSet),M_STOREVALUE(account.dept),M_STOREVALUE(account.companyId),M_STOREVALUE(account.loginName),M_STOREVALUE(account.yuLiuZiduan1),@(account.yuLiuZiduan2),M_STOREVALUE(account.yuLiuZiduan3)];
    }];
    return ret;
}
-(BOOL)deleAccount:(CAccountModel *)account
{
    if(account == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
//        ret  = [db executeQuery:@"DELETE from ACCOUNT where accountName = %@", account.userName];
        ret = [db executeUpdate:@"DELETE from ACCOUNT where accountName = ?", account.userName];
        
    }];
    return ret;
}
-(BOOL)updateAccount:(CAccountModel *)account
{
    if(account == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
        //        ret  = [db executeQuery:@"DELETE from ACCOUNT where accountName = %@", account.userName];
        ret = [db executeUpdate:@"UPDATE ACCOUNT SET passWord = ?,  gesturePassword = ?,  loginStatus = ?, showInList = ? ,userId = ?,info = ?,img = ?,msgNoticeSet = ?,dept = ?,companyId = ?, loginName = ? where accountName = ? ", account.passWord?account.passWord:@"", account.gesturePassword?account.gesturePassword:@"", account.recentlyLogin?@1:@0, account.isShowInList?@1:@0,M_STOREVALUE(account.userId),M_STOREVALUE(account.info),M_STOREVALUE(account.img),M_STOREVALUE(account.msgNoticeSet),M_STOREVALUE(account.dept),M_STOREVALUE(account.companyId),M_STOREVALUE(account.loginName), M_STOREVALUE(account.userName)];
    }];
    return ret;
}



-(CAccountModel *)getRecentlyLoginAccount
{
    
    __block CAccountModel *account = nil;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
        
        FMResultSet *result =  [db  executeQuery:@" SELECT * FROM ACCOUNT where  loginStatus = 1"];
        if(result != nil)
        {
            while ([result next]) {
                account = [[CAccountModel alloc] init];
                account.userName = [result stringForColumn:@"accountName"];
                account.passWord = [result stringForColumn:@"passWord"];
                account.gesturePassword = [result stringForColumn:@"gesturePassword"];
                account.recentlyLogin = [result boolForColumn:@"loginStatus"];
                account.isShowInList = [result boolForColumn:@"showInList"];
                account.info = [result stringForColumn:@"info"];
                account.userId = [result stringForColumn:@"userId"];
                account.img = [result stringForColumn:@"img"];
                account.msgNoticeSet = [result stringForColumn:@"msgNoticeSet"];
                account.dept = [result stringForColumn:@"dept"];
                account.companyId = [result stringForColumn:@"companyId"];
                account.loginName = [result stringForColumn:@"loginName"];

            }
        }
        [result close];
    }];
    return account;
}
-(NSArray *)getAccountNameList
{
    
    __block NSMutableArray *accountList = [NSMutableArray array];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
        
        FMResultSet *result =  [db  executeQuery:@" SELECT * FROM ACCOUNT where showInList = 1"];
        if(result != nil)
        {
            while ([result next]) {
                CAccountModel *account = [[CAccountModel alloc] init];
                account.userName = [result stringForColumn:@"accountName"];
                account.passWord = [result stringForColumn:@"passWord"];
                account.gesturePassword = [result stringForColumn:@"gesturePassword"];
                account.recentlyLogin = [result boolForColumn:@"loginStatus"];
                account.isShowInList = [result boolForColumn:@"showInList"];
                account.info = [result stringForColumn:@"info"];
                account.userId = [result stringForColumn:@"userId"];
                account.img = [result stringForColumn:@"img"];
                account.msgNoticeSet = [result stringForColumn:@"msgNoticeSet"];
                account.dept = [result stringForColumn:@"dept"];
                account.companyId = [result stringForColumn:@"companyId"];
                account.loginName = [result stringForColumn:@"loginName"];
                if (account.recentlyLogin) {
                    [accountList insertObject:account atIndex:0];
                }
                else
                {
                [accountList addObject:account];
                }
            }
        }
        [result close];
    }];
    
    return accountList;
}
-(CAccountModel *)getAccountByUserName:(NSString *)userName
{
    
    if(M_ISSTRNULL(userName))
    {
        return nil;
    }
    __block CAccountModel *account = nil;
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
        
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM ACCOUNT where  accountName = \'%@\'", userName];
        
        FMResultSet *result =  [db  executeQuery:sql];
        if(result != nil)
        {
            while ([result next]) {
                account = [[CAccountModel alloc] init];
                account.userName = [result stringForColumn:@"accountName"];
                account.passWord = [result stringForColumn:@"passWord"];
                account.gesturePassword = [result stringForColumn:@"gesturePassword"];
                account.recentlyLogin = [result boolForColumn:@"loginStatus"];
                account.isShowInList = [result boolForColumn:@"showInList"];
                account.info = [result stringForColumn:@"info"];
                account.userId = [result stringForColumn:@"userId"];
                account.img = [result stringForColumn:@"img"];
                account.msgNoticeSet = [result stringForColumn:@"msgNoticeSet"];
                account.dept = [result stringForColumn:@"dept"];
                account.companyId = [result stringForColumn:@"companyId"];
                account.loginName = [result stringForColumn:@"loginName"];
            }
        }
        [result close];
    }];
    return account;
}

- (BOOL)updateAllAcountStatus
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] commonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].commonShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE ACCOUNT SET loginStatus = 0"];
    }];
    return ret;

}
@end
